package cn.edu.lingnan.util;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.HashMap;

public class XmlHandler extends DefaultHandler {
    private HashMap<String,String> hm=new HashMap<>();
    private StringBuffer stringBuffer = new StringBuffer();

    public HashMap<String, String> getHm() {
        return hm;
    }
    
    //标注开始时执行吗，初始化操作
    @Override
    public void startElement (String uri, String localName,
                              String qName, Attributes attributes)
            throws SAXException
    {
        stringBuffer.delete(0,stringBuffer.length());
    }


    @Override
    //标注结束时执行，收尾操作,将对应的标签名与对应的值存入HanshMap
    public void endElement (String uri, String localName, String qName)
            throws SAXException
    {
        hm.put(qName.toLowerCase(),stringBuffer.toString().trim());
    }

    //读到字符数据时执行，要把读到的字符数据存起来
    @Override
    public void characters (char ch[], int start, int length)
            throws SAXException
    {
        stringBuffer.append(ch,start,length);

    }
}
